package com.ruoyi.store.controller;

import java.util.List;


import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.constant.BusinessAppContants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.store.domain.StoreApplication;
import com.ruoyi.store.domain.TStore;
import com.ruoyi.store.service.ITStoreService;
import com.ruoyi.store.service.StoreApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-11-16
 */
@RestController
@RequestMapping("/store")
public class StoreAppController extends BaseController
{
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    private StoreApplicationService storeApplicationService;

    @Autowired
    private ITStoreService itStoreService;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:application:list')")
    @GetMapping("/application/list")
    public TableDataInfo list(StoreApplication storeApplication)
    {
        startPage();
        List<StoreApplication> list = storeApplicationService.selectTBusinessApplicationList(storeApplication);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:application:export')")
    @Log(title = "导出店铺审核", businessType = BusinessType.EXPORT)
    @PostMapping("/application/export")
    public void export(HttpServletResponse response, StoreApplication storeApplication)
    {
        List<StoreApplication> list = storeApplicationService.selectTBusinessApplicationList(storeApplication);
        ExcelUtil<StoreApplication> util = new ExcelUtil<StoreApplication>(StoreApplication.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:application:query')")
    @GetMapping(value = "/application/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(storeApplicationService.selectTBusinessApplicationById(id));
    }

    @Log(title = "店铺审核操作", businessType = BusinessType.DELETE)
    @PutMapping("/updateAppState")
    public int updateAppState(@RequestBody StoreApplication storeApplication) {
        int row = storeApplicationService.updateTBusinessApplication(storeApplication);
        if(row>0){
            // 发件人邮箱和名称
            try {
                String text = "";
                // 创建一个邮件消息
                MimeMessage message = javaMailSender.createMimeMessage();
                // 创建 MimeMessageHelper
                MimeMessageHelper messageHelper = new MimeMessageHelper(message, false);
                messageHelper.setFrom("1206320238@qq.com", "xx跨境电商平台");
                // 收件人邮箱
                messageHelper.setTo(storeApplication.getEmail());
                // 邮件标题
                messageHelper.setSubject("店铺审核通知");
                // 邮件正文，第二个参数表示是否是HTML正文
                if(storeApplication.getState().equals(BusinessAppContants.PASS)){
                    TStore tStore = new TStore();
                    tStore.setStoreId(IdUtils.fastSimpleUUID());
                    tStore.setStoreAddress(storeApplication.getAddress());
                    tStore.setStoreEmail(storeApplication.getEmail());
                    tStore.setStorePhone(storeApplication.getPhone());
                    tStore.setBusinessId(storeApplication.getBusinessId());
                    tStore.setStoreOpeningDate(DateUtil.date(System.currentTimeMillis()));
                    itStoreService.insertTStore(tStore);
                    messageHelper.setText("您店铺审核已通过，将在1个工作日为您开通权限，请耐心等待!", false);
                }else {
                    messageHelper.setText("您店铺未通过审核，理由:"+storeApplication.getReason(), true);
                }
                messageHelper.setSentDate(DateUtils.getNowDate());
                // 发送
                javaMailSender.send(messageHelper.getMimeMessage());
            } catch (Exception e) {
                throw new ServiceException("邮件发送失败");
            }
        }

        return row;
    }

}
